﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lk.Api.Models;
using Lk.Common.Helper;
using Lk.Common.Result;
using Lk.IRepositories;
using Lk.IServices;
using Lk.Model.Dto;
using Lk.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Lk.Api.Controllers
{

    public class UserController : BaseApiController
    {


        private readonly IUserService _userService;
        private readonly IUserCourseVideoService _userCourseVideoService;

        public UserController(IUserService userService, IUserCourseVideoService userCourseVideoService)
        {
            _userService = userService;
            _userCourseVideoService = userCourseVideoService;
        }

     
        [HttpGet("list")]
        public ActionResult<IEnumerable<object>> Get()
        {
            var cliams = User.Claims.Select(t => new { t.Type, t.Value }).ToArray();
            return cliams;
        }

        [HttpGet, Route("info")]
        public async Task<ResultMessage<UsersDto>> GetUserInfo()
        {
            var result = await _userService.QueryByID(this.CurrentUserId);
            return new ResultMessage<UsersDto>(MapperHelper<users, UsersDto>.Map(result));
        }

        /// <summary>
        /// 初始加载
        /// </summary>
        [HttpGet, Route("init")]
        public async Task<VInitModel> InitLoad()
        {
            var users = await _userService.Query(s => s.unionid == UnionId).ConfigureAwait(false);
            var userInfo = users.FirstOrDefault();
            if (userInfo != null && userInfo.last_read_video_id != null)
            {
                var userVideo = await _userCourseVideoService.QueryByID(userInfo.last_read_video_id.Value);
                return new VInitModel()
                {
                    VideoDto = MapperHelper<user_course_video, VideoDto>.Map(userVideo)
                };
            }
            return null;

        }
    }
}